﻿using LPLS.Commons.DBModule;
using LPLS.DBEntities;
using LPLS.DBEntities.DbModels;
using LPLS.DBInterface;
using System.Collections.Generic;

namespace LPLS.DBServices.UserModule
{
    // 用户服务实现类
    // 用户仓库实现类
    public class UserRepository : IUserRepository
    {
        private readonly DBContext _dbContext;

        /// <summary>
        /// 数据库名称选择
        /// </summary>
        public string DataBaseName { get => DatabaseEnum.SqliteConfig.ToString(); }

        public UserRepository(DBContext dbContext)
        {
            _dbContext = dbContext;
        }

        public List<User> GetAllUsers()
        {
            return _dbContext.GetDbContext(DataBaseName).Queryable<User>().ToList();
        }

        public User GetUserById(int id)
        {
            return _dbContext.GetDbContext(DataBaseName).Queryable<User>().First(u => u.Id == id);
        }

        public void AddUser(User user)
        {
            _dbContext.GetDbContext(DataBaseName).Insertable(user).ExecuteCommand();
        }

        public void UpdateUser(User user)
        {
            _dbContext.GetDbContext(DataBaseName).Updateable(user).ExecuteCommand();
        }

        public void DeleteUser(int id)
        {
            _dbContext.GetDbContext(DataBaseName).Deleteable<User>().Where(u => u.Id == id).ExecuteCommand();
        }
    }
}